<template>
	<view v-if="authStatus == 1" bg-green-5 h-40px lh-40px pl-10px c-white text-lg>审核已通过</view>
	<view v-else-if="authStatus == 2" bg-red-5 h-40px lh-40px pl-10px c-white text-lg>审核被拒绝</view>
	<view class="page page-enterprise-auth">

		<view v-if="!authStatus" text-center>
			<view class="icon icon-audit text-6xl c-green-5" py-50px></view>
			<view text="xl">您的申请已提交审核，请耐心等待</view>
			<view pt-20px pb-100px c-gray-4>温馨提示：申请审核将在72时内完成</view>
			<view px-50px>
				<nut-button size="large" type="info" @click="back">返回</nut-button>
			</view>
		</view>
		<view v-else>
			<view pt10px pb20px class="license">
				<view pb14px>营业执照上传</view>
				<uni-file-picker return-type="object" v-model="image1" @select="(e) => select(e,1)" disable-preview :del-icon="false" :auto-upload="false">
					<view class="icon icon-upload text-5xl color-blue-5"></view>
					<view color-blue-5 text-lg>请上传营业执照</view>
				</uni-file-picker>
			</view>

			<view pt14px pb14px>
				<view pb14px>企业头像（建议尺寸 1:1）</view>
				<uni-file-picker return-type="object" v-model="image2" @select="(e) => select(e,0)" disable-preview :del-icon="false" :auto-upload="false">
					<view class="icon icon-upload text-3xl color-blue-5"></view>
					<view color-blue-5>请上传企业头像</view>
				</uni-file-picker>
			</view>

			<view flex items-center justify-between pt5px pb5px border-t>
				<view> <text color-red-5>*</text> 企业全称</view>
				<uni-easyinput color-gray v-model="name" :inputBorder="false" placeholder="请输入企业全称"></uni-easyinput>
			</view>
	<!-- 
			<view flex items-center justify-between pt5px pb5px border-t>
				<view> <text color-red-5>*</text> 企业简称</view>
				<uni-easyinput color-gray v-model="nickname" :inputBorder="false" placeholder="请输入企业简称"></uni-easyinput>
			</view> 
	-->

			<view flex items-center justify-between pt5px pb5px border-t>
				<view> <text color-red-5>*</text> 注册代码</view>
				<uni-easyinput color-gray v-model="code" :inputBorder="false" placeholder="请输入注册代码"></uni-easyinput>
			</view>

			<view flex items-center justify-between pt5px pb5px border-t>
				<view> <text color-red-5>*</text> 法人姓名</view>
				<uni-easyinput color-gray v-model="corporation" :inputBorder="false" placeholder="请输入法人姓名"></uni-easyinput>
			</view>
			
			<view flex items-center justify-between pt14px pb14px border-t>
				<view> <text color-red-5>*</text> 企业类型</view>
				<uni-data-picker :localdata="types" v-model="pickData" :map="{text:'title',value:'id'}" 
					:clear-icon="false" placeholder="请选择企业类型" popup-title="选择企业类型"
				></uni-data-picker>
			</view>

			<view fixed w-94vw bottom-20px left-3vw>
				<nut-button size="large" type="info" @click="submit">{{authStatus? '修改':'提交'}}企业认证</nut-button>
			</view>
		</view>
		
	</view>

	
</template>

<script setup>
	import api from '@/api'
	import { ref } from 'vue'
	import { useUserStore } from '@/stores/user'
	const user = useUserStore()

	const image1 = ref({})
	const image2 = ref({})
	let pic1 = ''
	let pic2 = ''

	const name = ref('')
	// const nickname = ref('')
	const code = ref('')
	const corporation = ref('')
	const pickData = ref('')
	const types = ref([])
	
	const authStatus = ref(10086)		//0待审核 1审核通过 2拒绝
	async function initData() {
		const list = await api.getEnterpriseType()
		types.value = list
		if(user.userInfo.type == 'employee') return
		const data = await api.getAuthInfo({
			member_type: 'boss'
		})
		if(data) {
			authStatus.value = data.status
			if(!data.status) return
			pickData.value = data.type
			name.value = data.firm_name
			corporation.value = data.firm_boss
			code.value = data.credit_code
			setImage(image1,data.business_license)
			setImage(image2,data.logo)
			pic1 = data.business_license
			pic2 = data.logo
		}
	}
	initData()

	const select = async ({tempFiles}, k) => {
		const file = tempFiles[0]
		const uploadRes = await api.uploadFile(file.url)
		if(k) {
			pic1 = uploadRes.images
		}else {
			pic2 = uploadRes.images
		}
	}

	const submit = async () => {

		if(!pic1 || !pic2){
			uni.showToast({
				title: '图片必须上传',
				icon: 'none'
			})
			return
		}

		// || !nickname.value
		if(!name.value || !code.value || !corporation.value || !pickData.value){
			uni.showToast({
				title: '所有选项必须填写',
				icon: 'none'
			})
			return
		}

		const id = await api.enterpriseAuth({
			business_license: pic1,
			logo: pic2,
			firm_name: name.value,
			credit_code: code.value,
			firm_boss: corporation.value,
			type: pickData.value,
		})
		if(id) {
			authStatus.value = 0
		}

	}

	const setImage = (image1,url) => {
		image1.value = {
			url,
			name: url.split('/').pop(),
			extname: url.split('.').pop()
		}
	}

	const back = () => {
		uni.navigateBack()
	}

	
</script>

<style lang="scss">
.page-enterprise-auth {
	padding: 10px 10px 120upx;
	.border-t{
		border-top: 1px solid #eee;
	}
	.uni-easyinput  {
		text-align: right;
	}

	.is-add {
		flex-direction: column;
	}

	.license .file-picker__box {
		width: 100%!important;
		padding: 80px 0;
	}

	.input-value-border {
		border: none;
	}
}

.uni-indexed-list__menu-item {
	background-color: white;
}
.uni-indexed-list {
	width: 70%;
	bottom: 60px!important;
	left: unset!important;
}

</style>
